package com.yc.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yc.bean.FoodBean;
import com.yc.bean.FoodTypeBean;
import com.yc.dao.FoodDao;
import com.yc.dao.FoodTypeDao;
import com.yc.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/foodType")
public class FoodTypeServlet extends BaseServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");

        List<FoodTypeBean> rs= FoodTypeDao.find();

        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        String json = objectMapper.writeValueAsString(rs);

        // 将 JSON 数据写入响应
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }
}
